package com.tuo51.foudation.exception;

import com.matt.foundation.model.ErrorCode;
import com.tuo51.foudation.model.StatusResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * *******************************
 * Created by Ron.
 * form com.tuo51
 * on 2020/12/30
 * *******************************
 */

@ControllerAdvice
@Slf4j
public class GlobalValidationExceptionHandler {

    @ExceptionHandler(BindException.class)
    @ResponseBody
    public StatusResponse handle(BindException exception) {
        exception.printStackTrace();
        String message = String.format("参数校验错误(%s)", exception.getMessage());
        return StatusResponse.error(ErrorCode.PARAM_CAN_NOT_BLANK, message );
    }
}